<?php
/*
 * 所属账号下的id
 */
function arruserid(){
    $type = D(C('TABLE_TYPE'));
    $type_result = $type->field('id')->where('userid=%d',session('userinfo')['id'])->select();
    if(!empty($type_result)){
    	$v = null;
    	foreach ($type_result as $k){
    		$v .= $k['id'].",";
    	}
    	$v = rtrim($v,",");
    	return $v;
    }else{
    	return null;
    }
}
/*
 * 所属账号下的显示条件
 */
function rwhere(){
	$arrid = null;
	$where = !empty($arrid) ? array('id'=>array('in',$arrid)) : null;
	return $where;
}
/*
 * 菜单递归显示
 * $len 递归层数
 * $pid 父ID
 */
function submenuninfo($len,$pid){
	$l = $len+1;
	$submenu = D(C('TABLE_SUBMENU'));
	$result = $submenu->where(array('mparent'=>$pid))->select();
	foreach($result as $v){
		$str .= "<option value='".$v['id']."'>".str_repeat('—',$l).$v['mtitle']."</option>".submenuninfo($l,$v['id']);
	}
	return $str;
}

/*
 * 改变菜单状态
 * $state 表名称
 */
function chagestateshow($state){
	if($state){
		return '开启';
	}else{
		return '<font color=\'#f00\'>关闭</font>';
	}
}

/*
 * 资源类型
 * $v 值
 */
function menutype($v=null){
	if(isset($v)){
		switch ($v){
			case 1:
				return '视频';
				break;
			case 2:
				return '咨询';
				break;
			case 3:
				return '频道';
				break;
			case 4:
				return '商城';
				break;
			default:
				return '视频';
		}
		
	}
}
/*
 * 菜单分类
 * $v 值
 */
function classtypename($v=null){
	if(isset($v)){
		switch ($v){
// 			case '':
// 				return '请选择';
// 				break;
			case 1:
				return '企业';
				break;
			case 2:
				return '政府';
				break;
			case 3:
				return '其他一';
				break;
			case 4:
				return '其他二';
				break;
			case 5:
				return '其他三';
				break;
			default:
				return '企业';
		}

	}
}
/*
 * 输出菜单下拉框
 */
function menuoption(){
	$submenu = D(C('TABLE_SUBMENU'));
	$result = $submenu->where("mtypeid=%d and mparent=%d",array(I('get.rootid'),0))->select();
	foreach($result as $v){
		$str .= "<option value='".$v['id']."'>".$v['mtitle']."</option>".submenuninfo(0,$v['id']);
	}
	return $str;
}
/*
 * 输出菜单类型
 */
function showmenutype(){
	$submenu = D(C('TABLE_SUBMENU'));
	$result = $submenu->where("id=%d",array(I('get.smid'),0))->field('mtype')->find();
	return $result['mtype'];
}
/*
 * 删除菜单
 */
function submenudel($id=null){
	if(isset($id)){
		$submenu = D(C('TABLE_SUBMENU'));
		$reult = $submenu->where(array('mparent'=>$id))->find();
		if(count($reult)>0){
			return 1; //有子菜单，先删除子菜单再删除本菜单
		}else{
			$reults = $submenu->delete($id);
			if($reults){
				return 2; //删除成功
			}else{
				return 3; //删除失败
			}
		}
	}else{
		return 4; //非法参数
	}
}
/*
 * 设置分页样式
 * $obj 传入分页对象
 */
function pagestyle($obj=null){
	if(isset($obj)){
		$Page = $obj;
		$Page->lastSuffix=false;
		$Page->setConfig('header','&nbsp;&nbsp;[共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页]');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('last','末页');
		$Page->setConfig('first','首页');
		$Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		return $Page;
	}
}
/*
 * 显示分页limit返回值
 */
function limitbakstr($obj=null){
	if(isset($obj)){
		return $obj->firstRow.','.$obj->listRows;
	}
}
/*
 * 数据为空时显示的内容
 */
function article_empty($var=null){
	if(isset($var)){
		switch ($var){
			case article:
				return "<div class='empty'>暂无数据，请点击<a onclick='javascript:history.go(-1);'>返回</a></div>";
				break;
			default:return "暂无数据";
		}
		
	}else{
		return '暂无数据！';
	}
}
/*
 * 菜单显示位置获取、设置
 * $result  结果集
 */
function menupostion($result){
	$positions = "";
	foreach($result as $v){
		$positions .= $v.",";
	}
	return rtrim($positions,',');
}
/*
 * 显示位置名称
 * $position_result  结果集
 */
function postionr($position_result){
	$position_result = explode(',', $position_result);
	$p = "";
	foreach($position_result as $v){
		$p .= pname($v)."、";
	}
	$p = rtrim($p,'、');
	if($p){
		return "(".$p.")";
	}	
}
function pname($r){
	if(!empty($r)){
		switch ($r){
			case 1:
				return "置顶";
				break;
			case 2:
				return "置底";
				break;
			case 3:
				return "置左";
				break;
			case 4:
				return "置右";
				break;
			default:
				return "";
		}
	}

}

function gettypeaddres($str,$id,$mtypeid=null){
	switch ($str){
		case 1:
			$p = U('Home/Index/list1',array('id'=>$id));
			break;
		case 2:
			$p = U('Home/Index/list2',array('id'=>$mtypeid,'shefid'=>$id));
			break;
		case 3:
			$p = U('Home/Index/list3',array('id'=>$id));
			break;
		case 4:
			$p = U('Home/Index/list4',array('id'=>$id));
			break;
		default:
			$p = U('Home/Index/list1',array('id'=>$id));
	}
	return $p;
}

/*
 * 字符串截取
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
	if(function_exists("mb_substr")){
		$slice= mb_substr($str, $start, $length, $charset);
	}elseif(function_exists('iconv_substr')) {
		$slice= iconv_substr($str,$start,$length,$charset);
	}else{
		$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
		$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
		$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
		$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
		preg_match_all($re[$charset], $str, $match);
		$slice = join("",array_slice($match[0], $start, $length));
	}
	$fix='';
	if(strlen($slice) < strlen($str)){
		$fix='...';
	}
	return $suffix ? $slice.$fix : $slice;
}

/*
 * 数组排序
 * asc是升序 desc是降序
 */
function arr_sort($array,$key,$order="asc"){
	$arr_nums=$arr=array();
	foreach($array as $k=>$v){
		$arr_nums[$k]=$v[$key];
	}
	if($order=='asc'){
		asort($arr_nums);
	}else{
		arsort($arr_nums);
	}
	foreach($arr_nums as $k=>$v){
		$arr[$k]=$array[$k];
	}
	return $arr;
}

//不同颜色
function imcolor(){
	$color = array(
		$colorarr1 = array('e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0','e67e22','3498db','2c3e50','5a68a5','525252','2ecc71','4288d0'),
		$colorarr2 = array('d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c','d35400','2980b9','2c3e50','46465e','333333','27ae60','124e8c')
	);
	return $color;
}


?>